package com.hotel.homehotel.before.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotel.homehotel.before.entiy.Comment;
import com.hotel.homehotel.before.entiy.Reply;
import com.hotel.homehotel.before.entiy.vo.CommentVo;
import com.hotel.homehotel.before.mapper.CommentMapper;
import com.hotel.homehotel.before.mapper.ReplyMapper;
import com.hotel.homehotel.before.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
@Service
public class CommentServiceImpl  implements CommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    ReplyMapper replyMapper;

    @Override
    public List<CommentVo> selectByRoomId(String roomId) {
        QueryWrapper queryWrapper1 = new QueryWrapper();
        if(!"-1".equals(roomId)) {

            queryWrapper1.eq("comment_room_id", roomId);
        }
        List<Comment> comment = commentMapper.selectList(queryWrapper1);

        List<CommentVo> cmo=new LinkedList<>();
        for(Comment c:comment) {
            QueryWrapper queryWrapper2=new QueryWrapper();
            queryWrapper2.eq("comment_id", c.getId());
            List<Reply> list = replyMapper.selectList(queryWrapper2);
            CommentVo commentVo = new CommentVo();
            commentVo.setId(c.getId());
            commentVo.setUid(c.getUid());
            commentVo.setRoomId(c.getRoomId());
            commentVo.setContent(c.getContent());
            commentVo.setCreatedBy(c.getCreatedBy());
            commentVo.setCreatedTime(c.getCreatedTime());
            commentVo.setReplies(list);
            cmo.add(commentVo);
        }
        return cmo;
    }

    @Override
    public boolean insertComment(Comment comment) {
      return commentMapper.insert(comment)>0;

    }

    @Override
    public boolean insertReply(Reply reply) {
        return  replyMapper.insert(reply)>0;
    }

    @Override
    public boolean deleteComment(String id) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("comment_id",id);
             if((commentMapper.deleteById(id)>0)&& (replyMapper.delete(queryWrapper)>0))
                 return true;
             return false;

    }

    @Override
    public boolean deleteReply(String id) {
       return replyMapper.deleteById(id)>0;
    }
}
